package com.qidian.mes.dv.service.Impl;

import com.qidian.mes.dv.mapper.MachineryTypeMapper;
import com.qidian.mes.dv.service.MachineryTypeService;
import com.qidian.mes.pojo.MachineryType;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 陆雯城
 * @Description
 * @Time 2024-04-11 16:11
 */
@Transactional
@Service
public class MachineryTypeServiceImpl implements MachineryTypeService {
    @Autowired
    private MachineryTypeMapper machineryTypeMapper;

    @Override
    public QdResult deleteByMachineryTypeId(Long machineryTypeId) {
        MachineryType machineryType = machineryTypeMapper.selectByMachineryTypeId(machineryTypeId);
        if (machineryType.getHasChildren()){
            return QdResult.error();
        }
        machineryTypeMapper.deleteByMachineryTypeId(machineryTypeId);
        Long parentTypeId = machineryType.getParentTypeId();
        int count = machineryTypeMapper.selectParentIdCountByMachineryTypeId(parentTypeId);
        if (count ==1){
            machineryTypeMapper.updateHasChildrenFalseByMachineryTypeId(parentTypeId);
        }
        return QdResult.success();
    }

    @Override
    public QdResult updateMachineryType(MachineryType machineryType) {
        MachineryType tempMachineryType = machineryTypeMapper.selectByMachineryTypeName(machineryType.getMachineryTypeName());
        if (tempMachineryType != null && !tempMachineryType.getMachineryTypeId().equals(machineryType.getMachineryTypeId())){
            return QdResult.duplicate("duplicate: machineryTypeName");
        }
        tempMachineryType = machineryTypeMapper.selectByMachineryTypeCode(machineryType.getMachineryTypeCode());
        if (tempMachineryType != null && !tempMachineryType.getMachineryTypeId().equals(machineryType.getMachineryTypeId())){
            return QdResult.duplicate("duplicate: machineryTypeCode");
        }
        Long newParentId = machineryTypeMapper.selectIdByMachineryTypeName(machineryType.getParentTypeName());
        Long oldParentId = machineryTypeMapper.selectByMachineryTypeId(machineryType.getMachineryTypeId()).getParentTypeId();
        if (!newParentId.equals(oldParentId)){
            //如果新的父类型没有孩子，则更新父类型的hasChildren为true
            MachineryType newParent = machineryTypeMapper.selectByMachineryTypeId(newParentId);
            if (!newParent.getHasChildren()){
                machineryTypeMapper.updateHasChildrenByMachineryTypeId(newParentId);
            }
            //如果旧的父类型只有一个有孩子，则更新父类型的hasChildren为false
            int count = machineryTypeMapper.selectParentIdCountByMachineryTypeId(oldParentId);
            if (count == 1){
                machineryTypeMapper.updateHasChildrenFalseByMachineryTypeId(oldParentId);
            }
        }
        machineryType.setParentTypeId(newParentId);
        return QdResult.isSuccess(machineryTypeMapper.updateMachineryType(machineryType));
    }

    @Override
    public QdResult saveMachineryType(MachineryType machineryType) {
        if (machineryTypeMapper.selectByMachineryTypeName(machineryType.getMachineryTypeName()) != null) {
            return QdResult.duplicate("duplicate: machineryTypeName");
        }
        if (machineryTypeMapper.selectByMachineryTypeCode(machineryType.getMachineryTypeCode()) != null) {
            return QdResult.duplicate("duplicate: machineryTypeCode");
        }
        Long parentId = machineryTypeMapper.selectIdByMachineryTypeName(machineryType.getParentTypeName());
        machineryType.setParentTypeId(parentId);
        machineryType.setHasChildren(false);
        int res = machineryTypeMapper.insertMachineryType(machineryType);
        int count = machineryTypeMapper.selectParentIdCountByMachineryTypeId(parentId);
        if (count >= 1) {
            machineryTypeMapper.updateHasChildrenByMachineryTypeId(parentId);
        }
        return QdResult.isSuccess(res);
    }

    @Override
    public QdResult queryMachineryTypeNames() {
        return QdResult.success(machineryTypeMapper.selectMachineryTypeNames());
    }

    @Override
    public QdResult queryMachineryTypeById(Long machineryTypeId) {
        MachineryType machineryType = machineryTypeMapper.selectByMachineryTypeId(machineryTypeId);
        String parentTypeName = machineryTypeMapper.selectByMachineryTypeId(machineryType.getParentTypeId()).getMachineryTypeName();
        machineryType.setParentTypeName(parentTypeName);
        return QdResult.success(machineryType);
    }

    @Override
    public QdResult<List<MachineryType>> queryAll() {
        return QdResult.success(machineryTypeMapper.selectAll());
    }

    //@Override
    //public QdResult<List<MachineryType>> queryByParentTypeId(Long parentTypeId,String machineryTypeName,Integer enableStatus) {
    //    return QdResult.success(machineryTypeMapper.selectByParentTypeId(parentTypeId,machineryTypeName,enableStatus));
    //}

    @Override
    public QdResult<List<MachineryType>> queryByParentTypeId(MachineryType machineryType) {
        return QdResult.success(machineryTypeMapper.selectByParentTypeId(machineryType));
    }
}
